//
//  AppDelegate.swift
//  LySwift微博
//
//  Created by 贠明东 on 15/11/22.
//  Copyright © 2015年 itcCZ. All rights reserved.
//

import UIKit
 
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //调用导航皮肤
        setupApp()
        
        print("加载账户信息：\(LyUserAccount.loadUserAccount())")
        
        ///创建Window(设置大小：铺满屏幕)
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //设置window的背景
        window?.backgroundColor = UIColor.whiteColor()
        
        ///创建TabBarVC
        let tabbarVC = LyMainViewController()
        
        
        //设置跟控制器(把TabBarVC添加入Window里)
//        window?.rootViewController = tabbarVC
        
        // 外部提供流水布局.但是不太好.流水布局需要设置参数,让调用的人来设置参数.封装的不是特别好
        window?.rootViewController = LyNewFeatureViewController()
        
        ///设置KeyWindow和显示
        // [window?—>如果window有值则显示(执行后面的代码)，没有值则不执行]
        window?.makeKeyAndVisible()
        
        print(ISNewFeature())
        
        return true
    }
    
    // MARK: - 判断是否是新版本
    /*
        判断是否是新版本
    
    - return : 是否是新版本
    */
    private func ISNewFeature() -> Bool {
        //获取当前版本
      let currentVersion = Double( NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        //获取上一次的版本(从偏好设置：都是储存小的数据)
        let sandboxVersionKey = "sandboxVersionKey"
        
        //上一次的版本号
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        
        //保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        print("当前的版本号=\(currentVersion)")
        print("上一次的版本号=\(sandboxVersion)")
        
        //比较当前版本和上一次的版本
        return currentVersion != sandboxVersion
    }
    
//    // MARK : - 判断进入哪个控制器
//    private func defaultController()-> UIViewController {
//        // 用户是否登录,没有加载到账号表示没有登录
//        if !LyUserAccount.userLogin {
//            // 显示访客视图
//            return LyMainViewController()
//    }
//
//        // 用户已经登录
//        // 是否是新版本,新版本返回新特新界面,否则返回欢迎界面
//        return isNewFeature() ? LyNewFeatureViewController() : LyWelcomeViewController()
//    }
    ///设置导航栏皮肤(定全局的botton的颜色)
    func setupApp()
    {
            let bar = UINavigationBar.appearance()
        //设置为橙色的botton
            bar.tintColor = UIColor.orangeColor()
            bar.backgroundColor = UIColor(red: 226 / 255.0, green: 2 / 255.0, blue: 114 / 255.0, alpha: 1)
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

